var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:"",
    tempFilePaths:'',
    myname:'',
    mypost:'',
    mytel:'',
    myheader:'',
    myhost: app.globalData.remoteDomainMy
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url:app.globalData.remoteDomainApi+'getmyinfo.php',
      data: {
        openid: app.globalData.openid,
        y: ''
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        let data = res.data;

        if(data !=null){
          that.setData({
            address:data.address,
            myname: data.uname,
            mypost: data.post,
            mytel: data.tel,
            myheader: data.header
          })
        }
      }
    })
  },
  /**
   * 提交表单
   */
  formSubmit:function(e){
    console.log(e);
    //获取表单数据

    //姓名
    let uname = e.detail.value.uname;

    //手机号
    let tel = e.detail.value.tel;

    //地址
    let address = e.detail.value.address;

    //邮编
    let post = e.detail.value.post;

    //获取头像临时地址
    let tempFilePaths = this.data.tempFilePaths;

    if (tempFilePaths.length>0){
      //执行上传+ 表单数据提交
      wx.uploadFile({
        url: app.globalData.remoteDomainApi + 'upload.php',
        filePath: tempFilePaths,
        name: 'file',
        formData: {
          uname3: uname,
          tel: tel,
          address: address,
          post: post,
          openid: app.globalData.openid
        },
        success(res) {
          const data = res.data;
          if (data == 2) {
            console.log('ok')
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
          else {
            wx.showToast({
              title: '抱歉，注册失败！',
              icon: 'none',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
        }
      })
    }else{//不选择头像
      wx.request({
        url: app.globalData.remoteDomainApi + 'upload.php',
        data: {
          uname3: uname,
          tel: tel,
          address: address,
          post: post,
          openid: app.globalData.openid
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          const data = res.data;
          if (data == 2) {
            wx.showToast({
              title: '注册成功',
              icon: 'success',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
          else {
            wx.showToast({
              title: '抱歉，注册失败！',
              icon: 'none',
              duration: 2000,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 2000)
              }
            })
          }
        }
      })
    }
   

  },

 /**
  * 获取位置
  */
  getLocation:function(){
    var that = this;

    wx.chooseLocation({
      success: function(res) {
        //地址
        let address = res.address;

        //渲染
        that.setData({
          address:address
        })
      },
    })
  },

  /**
   * 获取头像
   */
  getPhoto:function(){
    var that = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths

        //更新tempFilePath的值
        that.setData({
          tempFilePaths: tempFilePaths[0]
        })

      }
    })

  }
})